//
//  ViewController.m
//  IOStel
//
//  Created by 宋 on 15/11/30.
//  Copyright © 2015年 宋. All rights reserved.
//

#import "ViewController.h"
#import "AddViewController.h"
@interface ViewController ()<InputFinshDelegate>
{
    NSMutableArray * name;
    NSMutableArray * tel;

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"联系人";
    name = @[
             @"汪晓",
             @"宋京科"
             ].mutableCopy;
    tel = @[
            @"15200411916",
            @"18373358860"
            ].mutableCopy;
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:name forKey:@"name"];
    [def setObject:tel forKey:@"tel"];
    [def synchronize];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(onlin:)];
    
    
}

- (void)onlin:(UIBarButtonItem *)btn
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    btn.title = self.tableView.editing ? @"完成":@"编辑";
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return name.count;
    return tableView.isEditing ? name.count + 1: name.count;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row - name.count ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    if (indexPath.row < name.count) {
        cell.textLabel.text = name[indexPath.row];
        cell.detailTextLabel.text = tel[indexPath.row];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AddViewController * con = [[AddViewController alloc] initWithStyle:UITableViewStyleGrouped];
    if (indexPath.row < name.count) {
        con.names = name[indexPath.row];
        con.tels = tel[indexPath.row];
    }else
    {
        [name addObject:@""];
        [tel addObject:@""];
    }
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];
    if (tableView.editing) {
        [tableView setEditing:NO animated:YES];
    }
}

- (void)inputFinshed:(NSString *)names1 Finshedtel:(NSString *)tele
{
    NSMutableArray *arr = name.mutableCopy;
    name = arr;
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:name forKey:@"name"];
    
    
    NSIndexPath * indexPath = self.tableView.indexPathForSelectedRow;
    name[indexPath.row] = names1;
    tel[indexPath.row] = tele;
   
    [self.tableView reloadData];
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleInsert) {

    }else{
        [name removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
